package com.demo.web.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.demo.web.service.ITestServer;
import com.demo.web.service.impl.TestServer2Impl;
import com.jweb.framework.db.JdbcTemplate;
import com.jweb.framework.ioc.annotation.Autowired;
import com.jweb.framework.ioc.annotation.Value;
import com.jweb.framework.mvc.annotation.Controller;
import com.jweb.framework.mvc.annotation.Param;
import com.jweb.framework.mvc.annotation.Request;
import com.jweb.framework.mvc.bean.Result;
import com.jweb.plugins.druid.DruidPlugin;
import com.jweb.plugins.rpc.annotation.RpcConsumer;

/**
 * 测试Action
 * 
 * @author Yuanqy
 *
 */
@Controller
public class TestAction {

	private @Autowired ITestServer server1;// 通过接口注入
	private @Autowired("Server2") ITestServer server2;// 通过指定接口实现注入
	private @Autowired() TestServer2Impl server3;// 通过指定对象注入
	private @Value("rpc.registry.address") String registryAddress;// 注入配置文件
	private @Autowired DruidPlugin druid;// 插件注入
	private @RpcConsumer ITestServer server;// RPC客户端代理注入

	@Request.Get("/ioc")
	public Result ioc() {
		Result res = new Result(true);
		System.out.println("==通过接口注入");
		System.out.println(server1.getMsg());
		System.out.println("==通过指定接口实现注入");
		System.out.println(server2.getMsg());
		System.out.println("==通过指定对象注入");
		System.out.println(server3.getMsg());
		res.setData(server2.getMsg());
		return res;
	}

	@Request.Get("/jdbc")
	public Result jdbc() throws Exception {
		Result res = new Result(true);
		JdbcTemplate jdbc = druid.getJdbcTemplate();
		List<Map<String, Object>> list = jdbc.queryList("SELECT table_name FROM information_schema.tables", null);
		res.setData(list);
		return res;
	}

	@Request.Get("/rpc")
	public Result rpc() {
		Result res = new Result(true);

		res.setData(server.getMsg());

		return res;
	}

	@Request.Get("/param/{aa}")
	public Result param(HttpServletRequest req, String xb, @Param("aa") String aa) {
		Result res = new Result(true);
		System.err.println("req:" + req.getParameter("aa"));
		System.err.println("下标:" + xb);
		System.err.println("注解:" + aa);

		return res;
	}
}
